package order

import (
	"parking-lot-go-admin/controller"
	"parking-lot-go-admin/model"
	"parking-lot-go-admin/model/order"
	"strconv"
	"time"

	"parking-lot-go-admin/model/park"
	"strings"

	"github.com/gin-gonic/gin"
)

func List(c *gin.Context) {

	requestStruct := struct {
		CarNumber      *string   `json:"car_number"`
		CarNoColor     *int      `json:"car_no_color"`
		OrderType      *int      `json:"order_type"`
		ParkingLotName *string   `json:"parking_lot_name"`
		OrderNo        *string   `json:"order_no"`
		InTime         *[]string `json:"in_time"`
		Page           int       `json:"page" binding:"required"`
		Limit          int       `json:"limit" binding:"required"`
	}{}

	if err := c.ShouldBindJSON(&requestStruct); err != nil {
		controller.Fail(c, err.Error())
		return
	}

	start := (requestStruct.Page - 1) * requestStruct.Limit

	where := " status=1 "

	parkCodes := make([]string, 0)
	if requestStruct.ParkingLotName != nil && *requestStruct.ParkingLotName != "" {
		var parkingLotStruct park.ParkingLotStruct
		var parkingLotData []park.ParkingLotStruct
		model.GetModelData(
			model.Db,
			parkingLotStruct.TableName(),
			&parkingLotData,
			model.QueryOptions{
				Conditions: map[string]interface{}{
					"name like ?": "%%" + *requestStruct.ParkingLotName + "%%",
				},
			},
		)
		for _, v := range parkingLotData {
			parkCodes = append(parkCodes, v.ParkCode)
		}

		where += "and static_park_code in ("
		for _, v := range parkCodes {
			where += "'" + v + "',"
		}
		where = strings.TrimRight(where, ",")
		where += ")"
	}

	if requestStruct.CarNumber != nil && *requestStruct.CarNumber != "" {
		where += " and car_number like '%" + *requestStruct.CarNumber + "%'"
	}
	if requestStruct.CarNoColor != nil && *requestStruct.CarNoColor != -1 {
		where += " and car_no_color = " + strconv.Itoa(*requestStruct.CarNoColor)
	}
	if requestStruct.OrderType != nil && *requestStruct.OrderType != -1 {
		where += " and order_type = " + strconv.Itoa(*requestStruct.OrderType)
	}
	if requestStruct.OrderNo != nil && *requestStruct.OrderNo != "" {
		where += " and order_no like '%" + *requestStruct.OrderNo + "%'"
	}
	if requestStruct.InTime != nil && len(*requestStruct.InTime) > 0 {
		if len(*requestStruct.InTime) > 0 {
			where += " and in_time >= '" + (*requestStruct.InTime)[0] + "'"
		}
		if len(*requestStruct.InTime) > 1 {
			where += " and in_time <= '" + (*requestStruct.InTime)[1] + "'"
		}
	}

	//查询信息
	var orderStruct order.OrderStruct
	var orderData []order.OrderStruct
	if err := model.Db.Debug().Table(orderStruct.TableName()).Where(where).Order("id desc").Limit(requestStruct.Limit).Offset(start).Find(&orderData).Error; err != nil {
		controller.Fail(c, err.Error())
		return
	}

	var count int64
	if err := model.Db.Debug().Table(orderStruct.TableName()).Where(where).Count(&count).Error; err != nil {
		controller.Fail(c, err.Error())
		return
	}

	//数据格式化
	returnData := make([]map[string]interface{}, 0)
	for _, v := range orderData {

		var parkingLotStruct park.ParkingLotStruct
		if err := model.Db.Debug().Table(parkingLotStruct.TableName()).Where("park_code = ?", v.StaticParkCode).First(&parkingLotStruct).Error; err != nil {
			controller.Fail(c, err.Error())
			return
		}

		returnData = append(returnData, map[string]interface{}{
			"static_park_code": v.StaticParkCode,
			"parking_lot_name": parkingLotStruct.Name,
			"car_number":       v.CarNumber,
			"car_no_color":     v.CarNoColor,
			"car_type":         v.CarType,
			"order_no":         v.OrderNo,
			"order_type":       v.OrderType,
			"in_time":          isZeroTime(v.InTime),
			"out_time":         isZeroTime(v.OutTime),
		})
	}

	controller.Success(c,
		map[string]interface{}{
			"list":        returnData,
			"total":       count,
			"pageSize":    requestStruct.Limit,
			"currentPage": requestStruct.Page,
		})

}

func isZeroTime(inTime time.Time) interface{} {
	var outTime interface{}
	if inTime.IsZero() {
		outTime = ""
	} else {
		outTime = inTime.Format("2006-01-02 15:04:05")
	}
	return outTime
}
